package com.marktotop.controller.business;


import com.marktotop.entity.business.VaccinationSite;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.service.IVaccinationSiteService;
import com.marktotop.vo.business.VaccineAndSitesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器 - 接种地点管理
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Api(tags = "业务模块-接种点相关接口")
@RestController
@RequestMapping("/vaccine/site")
public class VaccinationSiteController {

    @Autowired
    private IVaccinationSiteService vaccinationSiteService;


    /**
     * 分页展示所有接种点信息，模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param site
     * @return
     */
    @ApiOperation(value = "接种点列表",notes = "分页展示所有接种点信息，模糊查询")
    @RequestMapping(value = "/findSiteList",method = RequestMethod.GET)
    @RequiresPermissions({"vaccineSite:show"})
    public ResultBean findSiteList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   VaccinationSite site){
        return vaccinationSiteService.findSiteList(pageNum,pageSize,site);
    }


    /**
     * 添加接种地点信息
     *
     * @param site
     * @return
     */
    @ApiOperation(value = "添加接种地",notes = "添加新的接种地信息")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @RequiresPermissions({"vaccineSite:add"})
    public ResultBean addVaccinationSite(@RequestBody @Validated VaccinationSite site) {
        if (!vaccinationSiteService.addVaccinationSite(site)){
            return ResultBean.error().message("添加接种地信息失败!");
        }
        return ResultBean.ok().message("添加接种地点信息成功!");
    }


    /**
     * 删除接种地点信息
     *       前提:接种地无进行的预约信息，并且没有关联的疫苗
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除接种地",notes = "删除接种地点信息")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @RequiresPermissions({"vaccineSite:delete"})
    public ResultBean delete(@PathVariable Long id) throws SystemException {
        if (!vaccinationSiteService.removeVaccinationSite(id)){
            return ResultBean.error().message("删除接种地点信息失败!");
        }
        return ResultBean.ok().message("删除接种地点信息成功!");
    }


    /**
     * 查询所有拥有某一疫苗的接种地点信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "所有接种地某疫苗",notes = "查询所有接种地点某一疫苗信息")
    @RequestMapping(value = "/getSiteByVid/{id}",method = RequestMethod.GET)
    public ResultBean getAllSiteByVid(@PathVariable Long id) {
        VaccineAndSitesVO allSiteByVid = vaccinationSiteService.getAllSiteByVid(id);
        if (null == allSiteByVid){
            return ResultBean.error().message("获取该疫苗的所有接种地点信息失败!");
        }
        return ResultBean.ok().data("vaccineInfo",allSiteByVid).message("获取该疫苗的所有接种地点信息失败成功!");
    }


    /**
     * 编辑接种地点信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑接种地",notes = "编辑接种地点信息")
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    @RequiresPermissions({"vaccineSite:edit"})
    public ResultBean editVaccinator(@PathVariable Long id) throws SystemException {
        VaccinationSite site = vaccinationSiteService.editVaccinationSite(id);
        return ResultBean.ok().message("获取要编辑的接种地点信息成功!").data("site",site);
    }


    /**
     * 更新接种地点信息
     *
     * @param site
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "更新接种地",notes = "更新接种地点信息")
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    @RequiresPermissions({"vaccineSite:update"})
    public ResultBean updateVaccinator(@RequestBody @Validated VaccinationSite site) {
        if (!vaccinationSiteService.updateById(site)){
            return ResultBean.error().message("更新接种地点信息操作成功!");
        }
        return ResultBean.ok().message("更新接种地点信息操作成功!");
    }
}
